package com.kgc.web;

import com.kgc.dao.CostDao;
import com.kgc.dao.paymentDao;
import com.kgc.entity.Building;
import com.kgc.entity.Cost;
import com.kgc.entity.Payment;
import com.kgc.utils.PageUtils;
import com.kgc.vo.R;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "costServlet", value = "/costServlet")
public class costServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter writer = response.getWriter();
        String opt = request.getParameter("opt");
        PageUtils pageUtils = new PageUtils();
        System.out.println("opt = " + opt);
        if ("cy1".equals(opt)) {
            String cy1name = request.getParameter("cy1name");
            String page = request.getParameter("page");
            String limit = request.getParameter("limit");
            int count = new paymentDao().count();

            if (page != null) {
                pageUtils.setCurrPageNo(Integer.parseInt(page));
            }
            if (limit != null) {
                pageUtils.setPageSize(Integer.parseInt(limit));
            }
            pageUtils.init(count);
            // 查询用户数据
            List<Payment> payments = new paymentDao().selectByPage(pageUtils, cy1name);
            writer.write(R.success("", count, payments).toJSON());
        } else if ("cy2".equals(opt)) {
            String type = request.getParameter("type");
            String page = request.getParameter("page");
            String limit = request.getParameter("limit");
            int count = new CostDao().count();
            if (page != null) {
                pageUtils.setCurrPageNo(Integer.parseInt(page));
            }
            if (limit != null) {
                pageUtils.setPageSize(Integer.parseInt(limit));
            }
            pageUtils.init(count);
            // 查询用户数据

            List<Cost> costs = new CostDao().selcetAllbytype(pageUtils, type);

            writer.write(R.success("", count, costs).toJSON());
        } else if ("id".equals(opt)) {
            Integer id = Integer.parseInt(request.getParameter("id"));
            int jiaofei = new CostDao().jiaofei(id);
            if(jiaofei != 0){
            int count = new CostDao().count();
            pageUtils.init(count);
            List<Cost> costs = new CostDao().selcetAll(pageUtils);
            writer.write(R.success("缴费成功", count, costs).toJSON());
            }else {
                writer.write(R.error("缴费失败，请刷新后重试").toJSON());
            }
        }else if ("cy1add".equals(opt)) {
            String costname = request.getParameter("costname");
            int count = new paymentDao().add(costname);
            if (count > 0) {
                writer.write(R.success("添加成功!").toJSON());
            } else {
                writer.write(R.error("添加失败!").toJSON());
            }
        }else if ("cy1delete".equals(opt)) {
          Integer id =Integer.parseInt(request.getParameter("id"));
            int count = new paymentDao().deleteById(id);
            if (count > 0) {
                writer.write(R.success("删除成功!").toJSON());
            } else {
                writer.write(R.error("删除失败,数据正在使用中...").toJSON());
            }
        } else if ("cy1edit".equals(opt)) {
            Integer id =Integer.parseInt(request.getParameter("id"));
            String name = request.getParameter("name");
            int count = new paymentDao().updateByID(id,name);
            if (count > 0) {
                writer.write(R.success("修改成功!").toJSON());
            } else {
                writer.write(R.error("修改失败...").toJSON());
            }
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }


}
